package com.sugar.psych.assessment.module.business.controller.app.usertreeholechatrecord;

import com.sugar.psych.assessment.framework.common.pojo.CommonResult;
import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.module.business.controller.app.usertreeholechatrecord.vo.AiChatMessageReqVO;
import com.sugar.psych.assessment.module.business.controller.app.usertreeholechatrecord.vo.AiChatMessageRespVO;
import com.sugar.psych.assessment.module.business.controller.app.usertreeholechatrecord.vo.AiChatRecordSimpleRespVO;
import com.sugar.psych.assessment.module.business.service.usertreeholechatrecord.UserTreeHoleChatRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.sugar.psych.assessment.framework.common.pojo.CommonResult.successApp;
import static com.sugar.psych.assessment.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "APP - 树洞AI聊天会话")
@RestController
@RequestMapping("/psych/user-tree-hole-chat-record")
@Validated
public class AppUserTreeHoleChatRecordController {

    @Resource
    private UserTreeHoleChatRecordService userTreeHoleChatRecordService;

    @PostMapping("/create")
    @Operation(summary = "聊天会话 - 创建")
    public CommonResult<Long> createAiChatRecord() {
        return successApp(userTreeHoleChatRecordService.createAiChatRecord(getLoginUserId()));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "聊天会话 - 删除")
    public CommonResult<Boolean> delAiChatRecord(@PathVariable("id")Long id) {
        userTreeHoleChatRecordService.delAiChatRecord(getLoginUserId(), id);
        return successApp(true);
    }

    @Operation(summary = "发送消息")
    @PostMapping(value = "/send-stream")
    public CommonResult<Boolean> sendChatMessageStream(@Valid @RequestBody AiChatMessageReqVO sendReqVO) {
        userTreeHoleChatRecordService.sendChatMessageStream(sendReqVO, getLoginUserId());
        return successApp(true);
    }

    @GetMapping("/get")
    @Operation(summary = "聊天消息 - 根据会话ID查当前会话的所有聊天消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<AiChatMessageRespVO>> getAiChatRecord(@RequestParam("id") Long id) {
        return successApp(userTreeHoleChatRecordService.getAiChatRecord(id));
    }

    @GetMapping("/page")
    @Operation(summary = "聊天会话 - 分页查")
    public CommonResult<PageResult<AiChatRecordSimpleRespVO>> appGetAiChatRecordPage(PageParam pageParam) {
        return successApp(userTreeHoleChatRecordService.appGetAiChatRecordPage(pageParam, getLoginUserId()));
    }

}